<?php

require_once ROOT_APPLICATION_MODULES_ADMIN_FORMS . DS . "Comment.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS . DS . 'Comment.php';
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . 'Comments.php';

require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "Users.php";

class Admin_CommentController extends Zend_Controller_Action {
    
    ///////////////////////
    // V A R I A B L E S //
    ///////////////////////
    private $modelComment;
    private $modelUser;
    
    /////////////////////
    // O V E R I D E S //
    ///////////////////// 
    public function init() {
        /* Initialize action controller here */
        if (Zend_Auth::getInstance()->getIdentity() == FALSE) {
            $this->_redirect('admin/auth/login');
        }
        $layoutPath = APPLICATION_PATH . '/templates/admin/default';
        $option = array('layout' => 'index',
            'layoutPath' => $layoutPath);

        Zend_Layout::startMvc($option);
        
        $this->modelComment = new End_Models_Comments();
        $this->modelUser = new End_Models_Users();

        //OTHER 
        $this->currentUser = $this->getInforUser();

        //VIEW 
        $this->view->currentUser = $this->currentUser;
        /* Initialize action controller here */
    }

    ///////////////////
    // U T I L I E S //
    ///////////////////
    private function getInforUser() {
        try {
            $currentUser = Zend_Auth::getInstance()->getIdentity();
            return $currentUser;
        } catch (Zend_Exception $ex) {
            
        }
    }
    
    ////////////////////
    // A C T I O N S  //
    //////////////////// 
    public function addAction() {
        //Create form enter info for a new comment
        $form = new Application_Form_Comment();

        //Set label for the button
        $form->submit->setLabel('Đồng ý');
        $form->cancel->setLabel('Hủy bỏ');

        //Set client view  form  = $form
        $this->view->form = $form;
        $this->view->bodyCopy = "<p >Nhập các thông tin vào form dưới đây:</p>";
        //$created_date = date('Y-m-d H:i:s');


        if ($this->getRequest()->isPost()) {
            $cancel = $this->getRequest()->getPost('cancel', '0');

            if ($cancel) {
                $this->_helper->redirector('index');
            }
            //Get data from form.
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {   //If data is valid 
                //Get data one by one'
                $title = $form->getValue('title');
                $content = $form->getValue('content');
                $full_name = $form->getValue('full_name');
                $web_site = "site";
                $email = "mail";
                $user_id = 1;
                $user_name = "useer";
                $page_url = "page";
                $ip = "ip";
                $created_date = '';
                $is_active = 1;
                $activate_date = '';
                $path = "path";
                $ordering = 1;
                $depth = 1;
                $reply = 1;
                //Create new Comment object from datas
                $comment = new Comment(null, $title, $content,
                                $full_name, $web_site, $email,
                                $user_id, $user_name, $page_url,
                                $ip, $created_date, $is_active,
                                $activate_date, $path, $ordering,
                                $depth, $reply);

                //Use insert function from Comment model
                $this->modelComment->_insert($comment);

                //Move to index
                $this->_helper->redirector('index');
            } else {
                //Return form
                $form->populate($formData);
            }
        }
    }

    public function deleteAction() {
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Yes') {
                $comment_id = (int) $this->getRequest()->getPost('comment_id');
                $modelComment->_delete($comment_id);
            }
            $this->_helper->redirector('index');
        } else {
            $comment_id = $this->_getParam('comment_id', 0);
            $this->view->Comments = Utility_Converter::objectToArray($this->modelComment->_find($comment_id));
        }
    }

    public function editAction() {
        $comment_id = $this->_getParam('comment_id');
        $form = new Application_Form_Comment();
        $form->submit->setLabel('Đồng ý');
        $form->cancel->setLabel('Hủy bỏ');
        $modified_date = date('Y-m-d H:i:s');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $cancel = $this->getRequest()->getPost('cancel', 0);
            if ($cancel) {
                $this->_helper->redirector('index');
            }
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {

                $title = $form->getValue('title');
                $content = $form->getValue('content');
                $comment = new Comment($comment_id, $title, $content, null, 0, 0, 0, 0, 0, 0, $modified_date, 0, 0, 0, 0, 0, 0); //dua thong tin vao doi tuong
                //tao doi tuong model voi cac phuong thuc xu li doi tuong o tren
                $this->modelComment->_update($comment);        //xu li doi tuong voi phuong thuc cua model
                $this->_helper->redirector('index');    //chuyen den trang index
            } else {
                $form->populate($formData);
            }
        } else {
            $comment_id = $this->_getParam('comment_id', 0);
            if ($comment_id > 0) {
                $form->populate(Utility_Converter::objectToArray($this->modelComment->_find($comment_id)));
            }
        }
    }

    public function indexAction() {
        $comment = $this->modelComment->fetchAll();
        $this->view->Comments = $comment;
    }

}

